package pl.edu.uj.tcs.blef.protocol;

/**
 * Client-Server protocol request /create-table factory.
 * @author Maciej Bendkowski
 *
 */
public final class CreateTableRequestFactory implements RequestFactory {

	@Override
	public Request build(Object[] parameters, RequestHandler handler) throws BuildFailedException {
		if(parameters.length != 2) throw new BuildFailedException("Two arguments expected.");
		try{
			String tableName = (String)parameters[0];
			String playerNick = (String)parameters[1];
			return new CreateTableRequest("/create-table "+tableName+" "+playerNick, handler);
		} catch(ClassCastException e) { throw new BuildFailedException(e.getMessage()); }
	}

	@Override
	public Request parse(String message, RequestHandler handler) throws ParseFailedException, BuildFailedException {
		if(message.split(" ").length != 3) throw new ParseFailedException("Two arguments expected.");
		if("/create-table".equals(message.split(" ")[0])){
			Object tab[] = new Object[2];
			tab[0] = message.split(" ")[1];
			tab[1] = message.split(" ")[2];
			return build(tab, handler);
		}
		else throw new ParseFailedException("Unknown request.");
	}

}
